﻿using System;
using System.Windows.Controls;
using System.Windows;

namespace SuperGo.Silverlight {
	public enum MessageBoxButtons {
		Ok,
		YesNo
	}

	public delegate void MessageBoxClosedDelegate(DialogResult result);

	public partial class MessageBox {
		public MessageBox() {
			try {
				InitializeComponent();
				
				// this line removed in Beta 2
				//DataContext = this;

				_Button1 = (Button)_goPanel.FindName("_Button1");
				_Button2 = (Button)_goPanel.FindName("_Button2");
				_text = (TextBlock)_goPanel.FindName("_text");

				Loaded += new RoutedEventHandler(MessageBox_Loaded);
			} catch (Exception ex) {
				HandleException(ex);
			}
		}

		MessageBoxClosedDelegate _messageBoxClosed;
		DialogResult _button1Result = DialogResult.OK;
		DialogResult _button2Result = DialogResult.OK;

		public bool ResultPending { get; private set; }

		void MessageBox_Loaded(object sender, RoutedEventArgs e) {
			try {
				_Button1.Click += new RoutedEventHandler(_Button1_Click);
				_Button2.Click += new RoutedEventHandler(_Button2_Click);
			} catch (Exception ex) {
				HandleException(ex);
			}
		}

		public void Show(string title, string text) {
			Show(title, text, MessageBoxButtons.Ok, null);
		}

		public void Show(string title, string text, MessageBoxButtons buttons, MessageBoxClosedDelegate messageBoxClosed) {
			try {
				_messageBoxClosed = messageBoxClosed;
				_goPanel.Title = title;
				_text.Text = text;

				switch (buttons) {
					case MessageBoxButtons.Ok:
						SetButtonProperties(_Button1, true, "Ok", 2);
						SetButtonProperties(_Button2, false);
						break;
					case MessageBoxButtons.YesNo:
						SetButtonProperties(_Button1, true, "Yes", 1);
						SetButtonProperties(_Button2, true, "No", 1);
						_button1Result = DialogResult.Yes;
						_button2Result = DialogResult.No;
						break;
					default:
						throw new Exception("Unsupported button type.");
				}

				// set ResultPending so Page knows not to hide this dialog until user has responded
				ResultPending = true;
			} catch (Exception ex) {
				HandleException(ex);
			}
		}

		private void UserHasResponded() {
			// set ResultPending so Page knows it can now hide this dialog again
			ResultPending = false;
			Hide();
		}
		
		protected void _Button2_Click(object sender, RoutedEventArgs e) {
			UserHasResponded();
			if (_messageBoxClosed != null)
				_messageBoxClosed(_button2Result);
		}

		protected void _Button1_Click(object sender, RoutedEventArgs e) {
			UserHasResponded();
			if (_messageBoxClosed != null)
				_messageBoxClosed(_button1Result);
		}

		private static void SetButtonProperties(ContentControl button, bool visible, string text, int colspan) {
			button.SetVisible(visible);
			if (visible) {
				button.Content = text;
				button.SetValue(Grid.ColumnSpanProperty, colspan);
			}
		}

		private static void SetButtonProperties(UIElement button, bool visible) {
			button.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
		}
	}
}
